package com.xmb.common.service.impl;

import com.xmb.common.dao.XmbUserScoreBlotterMapper;
import com.xmb.common.entity.XmbUserScoreBlotter;
import com.xmb.common.entity.reward.ActivityRanking;
import com.xmb.common.service.UserScoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * Created by lyndon on 2019/4/10.
 */
@Service
public class UserScoreServiceImpl implements UserScoreService{

    @Resource
    private XmbUserScoreBlotterMapper scoreBlotterMapper;

    /**
     * 获取用户的积分总金额
     * @param uid
     * @return
     */
    public BigDecimal getTotalScore(Long uid) {
        return Optional.ofNullable(scoreBlotterMapper.getTotalScore(uid)).orElse(BigDecimal.ZERO);
    }

    /**
     * 保存积分流水
     * @param record
     * @return
     */
    public int saveScoreBlotter(XmbUserScoreBlotter record) {
        return scoreBlotterMapper.insertSelective(record);
    }

    @Override
    public List<XmbUserScoreBlotter> getScoreList(Long userId, Long typeId, String exchangeType) {
        XmbUserScoreBlotter scoreBlotter = new XmbUserScoreBlotter();
        scoreBlotter.setExchangeType(exchangeType);
        scoreBlotter.setUserId(userId);
        scoreBlotter.setTypeId(typeId);
        return scoreBlotterMapper.getScoreList(scoreBlotter);
    }

    @Override
    public List<ActivityRanking> getScoreRanking(Long typeId) {
        return scoreBlotterMapper.getScoreRanking(typeId);
    }

    @Override
    public ActivityRanking getCurrentUserActivityRewardRanking(Long activityId, Long userId) {
        XmbUserScoreBlotter scoreBlotter = new XmbUserScoreBlotter();
        scoreBlotter.setUserId(userId);
        scoreBlotter.setTypeId(activityId);
        return scoreBlotterMapper.getCurrentUserActivityRewardRanking(scoreBlotter);
    }
}
